package org.example.domain.shop.service.discount.filter;


import org.example.domain.shop.adapter.repository.IShopRepository;
import org.example.domain.shop.model.entity.DiscountCalculationContextEntity;
import org.example.domain.shop.model.entity.DiscountCalculationLinkParamEntity;
import org.example.types.design.link.AbstractLogicLinkNode;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 * @author yinghuashen
 * @version 1.0
 * @description
 * @date 2025/3/8 00:04
 */

@Component
public class UserLevelFilter extends AbstractLogicLinkNode<DiscountCalculationLinkParamEntity, DiscountCalculationContextEntity, BigDecimal> {

    @Resource
    private IShopRepository shopRepository;

    @Override
    public BigDecimal execute(DiscountCalculationLinkParamEntity discountCalculationLinkParamEntity, DiscountCalculationContextEntity discountCalculationContextEntity) {
        // 1. 校验用户等级
        int level = shopRepository.getUserLevel(discountCalculationLinkParamEntity.getUserId());
        double discount = 0.0;
        switch (level) {
            case 1:
                discount = 20.0;
                break;
            case 5:
            case 6:
            case 7:
                discount = 10.0;
                break;
            default:
                discount = 0.0;
        }
        discountCalculationContextEntity.setDiscount(discountCalculationContextEntity.getDiscount().add(new BigDecimal(discount)));

        return discountCalculationContextEntity.getDiscount();
    }
}
